package com.example.noobpackage.ui.proxypackage

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.noobpackage.R
import com.example.noobpackage.logic.model.ApplicationUser
import com.example.noobpackage.tool.MyApplication
import com.example.noobpackage.ui.MainViewModel
import com.example.noobpackage.ui.login.LoginFragment
import com.example.noobpackage.ui.util.LoginRequiredFragment
import kotlinx.android.synthetic.main.accepted_package_item.*
import kotlinx.android.synthetic.main.fragment_proxy_package.*

class ProxyPackageFragment : LoginRequiredFragment() {

    private val shareViewModel: MainViewModel by activityViewModels()
    private val viewModel: ProxyPackageViewModel by viewModels()

    private val adapter = ProxyPackageAdapter()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        setupReturn()
        return inflater.inflate(R.layout.fragment_proxy_package, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loginJudge { user ->
            val layoutManager = LinearLayoutManager(MyApplication.context)
            proxyList.layoutManager = layoutManager
            proxyList.adapter = adapter

            swipeRefresh.setColorSchemeResources(R.color.green_500)
            // 触发数据更新
            swipeRefresh.isRefreshing = true
            swipeRefresh.setOnRefreshListener {
                viewModel.updateList(user.account)
            }
            viewModel.updateList(user.account)
            // 数据更新
            viewModel.getList().observe(viewLifecycleOwner) { list ->
                adapter.setExpressData(list)
                swipeRefresh.isRefreshing = false
            }
        }
    }
}